package com.hig.ledger.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hig.ledger.mapper.LedgerWorkPermitMapper;
import com.hig.ledger.domain.LedgerWorkPermit;
import com.hig.ledger.service.ILedgerWorkPermitService;

/**
 * 特种作业许可证清单Service业务层处理
 *
 * @author scp
 * @date 2024-09-22
 */
@Service
public class LedgerWorkPermitServiceImpl implements ILedgerWorkPermitService
{
    @Autowired
    private LedgerWorkPermitMapper ledgerWorkPermitMapper;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询特种作业许可证清单
     *
     * @param id 特种作业许可证清单ID
     * @return 特种作业许可证清单
     */
    @Override
    public LedgerWorkPermit selectLedgerWorkPermitById(Long id)
    {
        return ledgerWorkPermitMapper.selectLedgerWorkPermitById(id);
    }

    /**
     * 查询特种作业许可证清单列表
     *
     * @param ledgerWorkPermit 特种作业许可证清单
     * @return 特种作业许可证清单
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<LedgerWorkPermit> selectLedgerWorkPermitList(LedgerWorkPermit ledgerWorkPermit)
    {
        return ledgerWorkPermitMapper.selectLedgerWorkPermitList(ledgerWorkPermit);
    }

    /**
     * 新增特种作业许可证清单
     *
     * @param ledgerWorkPermit 特种作业许可证清单
     * @return 结果
     */
    @Override
    public int insertLedgerWorkPermit(LedgerWorkPermit ledgerWorkPermit)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        ledgerWorkPermit.setCreateBy(loginUser.getUser().getUserName());
        ledgerWorkPermit.setDeptId(loginUser.getUser().getDeptId());
//        long id = ledgerWorkPermitMapper.getNextId();
//        ledgerWorkPermit.setId(id);
        ledgerWorkPermit.setCreateTime(DateUtils.getNowDate());
        String fileUrl = ledgerWorkPermit.getFileUrl();
        String[] fileUrls = fileUrl.split(",");
        String fileName = "";
        for (String url : fileUrls){
            int lastIndex = url.lastIndexOf("/");
            if (lastIndex != -1) {
                String name = url.substring(lastIndex + 1);
                fileName += name + ",";
            }
        }
        if (fileName.length()>0){
            fileName = fileName.substring(0,fileName.length()-1);
        }
        ledgerWorkPermit.setFileName(fileName);
        return ledgerWorkPermitMapper.insertLedgerWorkPermit(ledgerWorkPermit);
    }

    /**
     * 修改特种作业许可证清单
     *
     * @param ledgerWorkPermit 特种作业许可证清单
     * @return 结果
     */
    @Override
    public int updateLedgerWorkPermit(LedgerWorkPermit ledgerWorkPermit)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        ledgerWorkPermit.setUpdateBy(loginUser.getUser().getUserName());
        ledgerWorkPermit.setUpdateTime(DateUtils.getNowDate());
        String fileUrl = ledgerWorkPermit.getFileUrl();
        String[] fileUrls = fileUrl.split(",");
        String fileName = "";
        for (String url : fileUrls){
            int lastIndex = url.lastIndexOf("/");
            if (lastIndex != -1) {
                String name = url.substring(lastIndex + 1);
                fileName += name + ",";
            }
        }
        if (fileName.length()>0){
            fileName = fileName.substring(0,fileName.length()-1);
        }
        ledgerWorkPermit.setFileName(fileName);
        return ledgerWorkPermitMapper.updateLedgerWorkPermit(ledgerWorkPermit);
    }

    /**
     * 批量删除特种作业许可证清单
     *
     * @param ids 需要删除的特种作业许可证清单ID
     * @return 结果
     */
    @Override
    public int deleteLedgerWorkPermitByIds(Long[] ids)
    {
        return ledgerWorkPermitMapper.deleteLedgerWorkPermitByIds(ids);
    }

    /**
     * 删除特种作业许可证清单信息
     *
     * @param id 特种作业许可证清单ID
     * @return 结果
     */
    @Override
    public int deleteLedgerWorkPermitById(Long id)
    {
        return ledgerWorkPermitMapper.deleteLedgerWorkPermitById(id);
    }
}
